package com.tc.travel.submitter.executor;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.math.NumberUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;

import com.tc.travel.submitter.service.SessionBo;
import com.tc.travel.submitter.service.VisaBo;
import com.tc.travel.util.Consts;
import com.tc.travel.util.HttpClientUtil;
import com.tc.travel.util.PropertyUtil;
import com.tc.travel.util.StringUtil;
import com.tc.travel.util.exception.ComRuntimeException;

public class BaseExecutor {
	// private static Logger log = Logger.getLogger(BaseExecutor.class);

	@Autowired
	protected SessionBo sessionBo;

	@Autowired
	protected VisaBo visaBo;

	protected void openSearchVisaPage(String cookie) {
		String url = PropertyUtil.destUrlVisaSearch;

		Map<String, String> params = new HashMap<String, String>();
		// params.put("mode", "add");

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Cookie", cookie);
		headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
		headers.put("Accept-Encoding", "gzip, deflate, br");
		headers.put("Accept-Language", "en,ja;q=0.9,en-US;q=0.8");
		headers.put("Connection", "keep-alive");
		headers.put("Host", "churenkyosystem.com");
		headers.put("Referer", "https://churenkyosystem.com/member/top.php");
		headers.put("Upgrade-Insecure-Requests", "1");
		headers.put("User-Agent", Consts.USER_AGENT);

		HttpClientUtil.doGet(url, params, headers);
	}

	protected String getVisaIdentityId(String destVisaId, String cookie) {
		String url = PropertyUtil.destUrlVisaSearch;
		Map<String, String> params = new HashMap<String, String>();
		params.put("PUBLISH_DATE_FROM", "");
		params.put("PUBLISH_DATE_TO", "");
		params.put("DIPLOMAT_ID", "");
		params.put("CODE", destVisaId);
		params.put("CHINA_AGENT_ID", "");
		params.put("CHINA_AGENT_CODE", "");
		params.put("viewAgent_company", "");
		params.put("viewAgent_diplomat", "");
		params.put("VISA_TYPE", "");
		params.put("VISA_TYPE_1", "");
		params.put("ARRIVAL_DATE_FROM", "");
		params.put("ARRIVAL_DATE_TO", "");
		params.put("DEPARTURE_DATE_FROM", "");
		params.put("DEPARTURE_DATE_TO", "");
		params.put("CANCEL_TYPE", "");
		params.put("PUBLISH_STATUS", "");
		params.put("PAGE_VIEW_NUMBER", "0");
		params.put("BTN_SEARCH_x", "検 索");

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Cookie", cookie);
		headers.put("Accept:text/html", "application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
		headers.put("Accept-Encoding", "gzip, deflate, br");
		headers.put("Accept-Language", "en,ja;q=0.9,en-US;q=0.8");
		headers.put("Cache-Control", "max-age=0");
		headers.put("Connection", "keep-alive");
		headers.put("Content-Type", "application/x-www-form-urlencoded");
		headers.put("Host", "churenkyosystem.com");
		headers.put("Origin", "https://churenkyosystem.com");
		headers.put("Referer", "https://churenkyosystem.com/member/identity_list.php");
		headers.put("Upgrade-Insecure-Requests", "1");
		headers.put("User-Agent",Consts.USER_AGENT);

		String html = HttpClientUtil.doPost(url, params, headers);
		String identityId = extractIdentityId(html, destVisaId);
		if (StringUtil.isEmpty(identityId)) {
			throw new ComRuntimeException("no identity id found.");
		}
		return identityId;
	}

	private String extractIdentityId(String html, String destVisaId) {
		if (StringUtil.isEmpty(html)) {
			throw new ComRuntimeException("getVisaIdentityId error; returned html is null.");
		}

		try {
			Document doc = Jsoup.parse(html);
			Elements links = doc.select(":containsOwn(" + destVisaId + ")");
			for (Element link : links) {
				String href = link.attr("href");
				if (href != null) {
					String key = "IDENTITY_ID=";
					int idx = href.indexOf(key);
					if (idx >= 0) {
						String identityId = href.substring(idx + key.length()).trim();
						if (NumberUtils.isDigits(identityId)) {
							return identityId;
						}
					}
				}
			}
		} catch (Exception e) {
			throw new ComRuntimeException("extractIdentityId error;", e);
		}

		return null;
	}

}
